package com.klikmee.sms.core.base.communicator.mtel;

import hr.tis.mgw.client.GatewayException;
import hr.tis.mgw.client.message.DeliveryReport;
import hr.tis.mgw.client.message.MessageHelper;
import hr.tis.mgw.client.message.MessageManager2;
import hr.tis.mgw.client.message.ReceivedSms;

import org.apache.log4j.Logger;


/**
 * Implementation of {@link MTelMessageRetreiver} which uses {@link MessageManager2#receiveSmsMessages(String) to retreive messages or delivery report.
 *
 */
public class MessageManager2RetrieverImpl implements MTelMessageRetreiver{
	
	/**
	 * Reference to log object
	 */
	private Logger logger = Logger.getLogger( MessageManager2RetrieverImpl.class );
	
	/**
	 * Applications registration id at MTel mobile operater
	 */
	private String registrationId = null;
	
	/**
	 * Reference to the {@link MessageManager2} implementation used to retreive info from MTel
	 */
	private MessageManager2 messageManager2 = null;
	
	/**
	 * Implementation of {@link MTelCommunicator} responsible for handling messages and delivery reports
	 */
	private MTelCommunicator mTelCommunicator = null;
	
	/**
	 * This method should be called periodically to retrieve messages from MTel.
	 * After messages are received their type is inspected and depending on that forwarded to {@link MTelCommunicator#onDeliveryReport(hr.tis.mgw.client.message.DeliveryReport)}
	 * or {@link MTelCommunicator#onSmsMessage(hr.tis.mgw.client.message.ReceivedSms)}
	 */
	@Override
	public void getMessagesOrDeliveryReports() {
		// get info from MTel
		try {
			ReceivedSms[] receiveSmsMessages = this.messageManager2.receiveSmsMessages( this.registrationId );
			
			logger.debug( String.format( "Received %s messages from mtel", receiveSmsMessages.length ) );
			
			// determine actual type of received sms's and forward them accordingly
			for (int i = 0; i < receiveSmsMessages.length; i++) {
				
				ReceivedSms receivedSms = receiveSmsMessages[i];
				
				if(MessageHelper.isDeliveryReport( receivedSms )){
					// in case of delivery report 
					DeliveryReport deliveryReport = MessageHelper.createDeliveryReport( receivedSms );
					
					this.mTelCommunicator.onDeliveryReport( deliveryReport );
					
					logger.debug( String.format( "Delivery report with message id [%s] delivered to mtel communicator.", deliveryReport.getMessageId() ) );
				}
				
				// in case of regular sms
				else {
					
					this.mTelCommunicator.onSmsMessage( receivedSms );
					logger.debug( String.format( "Sms message with from  [%s] delivered to communicator.", receivedSms.getSenderAddress() ) );
				}
			}
			
		} catch (GatewayException e) {
			//TODO: implement exception handling
		}
		
	}
	
	public void setMessageManager2( MessageManager2 messageManager2 ) {
		this.messageManager2 = messageManager2;
	}
	
	public MessageManager2 getMessageManager2() {
		return messageManager2;
	}
	
	public void setRegistrationId( String registrationId ) {
		this.registrationId = registrationId;
	}
	
	public void setmTelCommunicator( MTelCommunicator mTelCommunicator ) {
		this.mTelCommunicator = mTelCommunicator;
	}

}
